//
//  StatusPictureView.swift
//  WeChat
//
//  Created by 梁迪 on 15/12/20.
//  Copyright © 2015年 梁迪. All rights reserved.
//

import UIKit
import SnapKit
private let pictureCellMargin:CGFloat = 5
private let pictureCellID = "pictureCellID"


class StatusPictureView: UICollectionView {
    //定义外部模型属性
    var imageURLs:[NSURL]?{
        
        didSet{
            
            let pSize = caclePictureViewSize()
            print(pSize)
            
            //更新配图的约束
            //如果视图对应的属性有更新就直接更新
            //如果没有该约束就创建
          self.snp_updateConstraints { (make) -> Void in
            make.size.equalTo(pSize)
            }
            
            testLabel.text = "\(imageURLs?.count ?? 0)"
            
            //刷新列表
            reloadData()
        }
    }
    
    //计算配图视图的大小
    private func caclePictureViewSize() -> CGSize {
        let imageCount = imageURLs?.count ?? 0
        let maxWidth = screenW - 2 * StatusCellMarigin

        let ItemWidth = (maxWidth - 2 * pictureCellMargin) / 3
        
        //取出collectionView 的流水布局
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        //设置 item size
        layout.itemSize = CGSize(width: ItemWidth, height: ItemWidth)
        
        if imageCount == 0 {
            return CGSizeZero
        }
        
        if imageCount == 1 {
            let imageSize = CGSize(width: 180, height: 120)
            //设置单张图片的 item size
            layout.itemSize = imageSize
            return imageSize
        }
        if imageCount == 4 {
            let w = ItemWidth * 2 + pictureCellMargin
            return CGSize(width: w, height: w)
            
            
        }
        
        
        let row = CGFloat((imageCount - 1) / 3 + 1 )
        
        return CGSize(width: maxWidth, height: row * ItemWidth + (row - 1) * pictureCellMargin)
        
        
        
        
        
        
        
        
    }
    

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        let flowLayout = UICollectionViewFlowLayout()
        
        flowLayout.minimumInteritemSpacing = pictureCellMargin
        flowLayout.minimumLineSpacing = pictureCellMargin
        
        super.init(frame: frame, collectionViewLayout: flowLayout)
        backgroundColor = UIColor.whiteColor()
        //注册 cell
        self.registerClass(PictureCell.self, forCellWithReuseIdentifier: pictureCellID)
        //设置数据源代理
        self.dataSource = self
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        self.addSubview(testLabel)
        testLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
            
        }
        
        
        
    }
    
    
    //懒加载
    private lazy var testLabel:UILabel = UILabel(title: "", color: UIColor.redColor(), fontSize: 30)

}


extension StatusPictureView:UICollectionViewDataSource{
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageURLs?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureCellID, forIndexPath: indexPath) as! PictureCell
//        cell.backgroundColor = UIColor.randomColor()
        cell.imageURL = imageURLs![indexPath.item]
        
        return cell
        
        
        
        
    }
    
}

class PictureCell :UICollectionViewCell {
    //定义外部模型属性
    var imageURL:NSURL?{
        didSet{
           iconView.sd_setImageWithURL(imageURL)
            
        }
        
        
    }
    
    override init(frame: CGRect) {
        super.init(frame:frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        contentView.addSubview(iconView)
        iconView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(snp_edges)
        }
        
        
    }
    
    
    
    
    //懒加载
    private lazy var iconView:UIImageView = {
        let vi = UIImageView()
        vi.contentMode = UIViewContentMode.ScaleAspectFill
        //在手码开发中,图片裁剪默认是不开启的
        
        vi.clipsToBounds = true
        
        return vi
        
        
        
        
    }()
    
    
}









